package com.code.question4.client;

import com.alibaba.fastjson.JSON;
import com.code.question4.base.RequestType;
import com.code.question4.base.ResponseType;
import com.code.question4.model.ResponseMessage;
import com.code.question4.model.UserMessage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器端
 */
public class ServerClient {
    public static void main(String[] args) {
        //申明服务端Socket与客户端对象
        ServerSocket ss = null;
        Socket accept = null;
        //申明接收信息与发送消息的对象
        BufferedReader bufferedReader = null;
        PrintStream ps = null;
        try {
            //开启监听端口8888等待客户端连入
            ss = new ServerSocket(8888);
            System.out.println("等待客户端连接");
            accept = ss.accept();
            System.out.println("客户端" + accept.getPort()+","+accept.getLocalPort());

            //获取客户端信息与输入输出流对象
            bufferedReader = new BufferedReader(new InputStreamReader(accept.getInputStream()));
            ps = new PrintStream(accept.getOutputStream());
            String response = bufferedReader.readLine();
            InetAddress inetAddress = accept.getInetAddress();
            System.out.println("客户端" + inetAddress + "发来的字符串内容是：" + response);
            //将接收到的客户端信息Json格式序列化为对象
            UserMessage message = JSON.parseObject(response, UserMessage.class);
            //做数据校验 判断是否为指定的调用方式与接口参数
            if (response != null && message.getCode().equals(RequestType.GET) && message.getTag().equals("login") && message.getUser() != null) {
                if (message.getUser().getName().equals("admin") && message.getUser().getPassword().equals("123456")) {
                    ResponseMessage rm = new ResponseMessage(ResponseType.SUCCESS, "success,login");
                    ps.println(JSON.toJSON(rm));
                } else {
                    ResponseMessage rm = new ResponseMessage(ResponseType.FAIL, "fail,account or password wrong.");
                    ps.println(JSON.toJSON(rm));
                }
            } else {
                ResponseMessage rm = new ResponseMessage(ResponseType.FAIL, "fail,parameter wrong.");
                ps.println(JSON.toJSON(rm));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭对象
            ps.close();
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != accept) {
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != ss) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
